package com.example.speeddating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.constants.TaoHuaConstant;
import com.example.speeddating.entity.Article;
import com.example.speeddating.entity.Comment;
import com.example.speeddating.mapper.CommentMapper;
import com.example.speeddating.mapper.LikeMapper;
import com.example.speeddating.mapper.UserMapper;
import com.example.speeddating.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.speeddating.service.TaohuaDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:33
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private TaohuaDetailService taohuaDetailService;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private UserMapper userMapper;

    private Long curUserId;

    @Override
    public Page getTopCommentsByArticleId(Page<Comment> page,Long articleId,Long curUserId) {
        Page<Comment> commentPage = commentMapper.findTopCommentsByParentIdAndArticleId(page, -1L, articleId);
        this.curUserId = curUserId;
        return getParent(commentPage);
    }

    private Page getParent(Page<Comment> page){
        List<Comment> rawComments = page.getRecords();
        for (Comment topComment : rawComments) {  //遍历顶级评论
            LinkedList<Comment> comments = new LinkedList<>();
            List<Comment> replyCommentsByTopComment = topComment.getReplyComments();
            for (Comment replyComment : replyCommentsByTopComment) { //遍历顶级评论的回复列表
                handleChild(replyComment,comments);//处理子孙评论
            }
            topComment.setReplyComments(comments);
            topComment.setIsLike(likeMapper.selectIsLike(topComment.getId(),curUserId));
        }
        page.setRecords(rawComments);
        return page;
    }

    private void handleChild(Comment replyComment,List<Comment> parent){
        List<Comment> grandChildren = replyComment.getReplyComments();
        replyComment.setIsLike(likeMapper.selectIsLike(replyComment.getId(),curUserId));
        replyComment.setReplyComments(null);
        parent.add(replyComment);
        for (Comment grandChild : grandChildren) {
            grandChild.setIsLike(likeMapper.selectIsLike(grandChild.getId(),curUserId));
            grandChild.setReplyNickname(replyComment.getNickname());
            if (grandChild.getReplyComments()!=null)handleChild(grandChild,parent);
        }
    }

    @Override
    public Page selectCommentByArticleId(Page<Comment> page,Long articleId) {
        return commentMapper.findTopCommentsByParentIdAndArticleId(page, -1L, articleId);
    }

    @Override
    public Integer selectArticleCommentNum(long articleId) {
        return commentMapper.selectArticleCommentNum(articleId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void publishComment(Comment comment) {
        taohuaDetailService.insertTaohuaDetail(TaoHuaConstant.Type_3,comment.getUserId());
        commentMapper.insert(comment);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteCommentById(Long id) {
        commentMapper.deleteById(id);
    }

    @Override
    public Comment selectComment(Long id) {
        Comment comment = commentMapper.selectComment(id);
        String avator = userMapper.selectUserAvator(comment.getUserId());
        String name = userMapper.selectUserNickname(comment.getUserId());
        comment.setAvator(avator);
        comment.setNickname(name);
        comment.setReplyComments(new ArrayList<>());
        if (comment.getParentCommentId()!=-1){
            String nickname = commentMapper.selectReplyNickname(comment.getParentCommentId());
            comment.setReplyNickname(nickname);
        }
        return comment;
    }

}
